 

##------------------------------------------------------------------
##
## Load libraries
##
##------------------------------------------------------------------
library("tidyverse")
library("dplyr")
library("knitr")
library("kableExtra")
library("metafor")
library("janitor")
library("data.table")
library("tools")
library("stringr")
library("lubridate")
library("svglite")
library("ggplot2")
library("ggthemes")
options(width=200,scipen=999)

##------------------------------------------------------------------
##
## Read CSV files
##
##------------------------------------------------------------------

setwd("C:/Users/awjor/Dropbox/misconduct/Replication")
library(ggplot2)

# Create data for the timeline
timeline_data <- data.frame(
  period = c("Period 1: Sep 2006 - Jul 2008", "Period 2: Sep 2014 - Dec 2018"),
  start_date = as.Date(c("2006-09-01", "2010-09-01")),
  end_date = as.Date(c("2008-07-31", "2014-12-31"))
)

# Event data
event_data_1 <- data.frame(
  event = "Laquan MacDonald Scandal: Nov 2015",
  date = as.Date("2011-12-01"),
  ypos = 0.5
)

event_data_2 <- data.frame(
  event = "IPRA Switches to COPA: Sep 2017",
  date = as.Date("2013-09-01"),
  ypos = 0.35
)

event_data_3 <- data.frame(
  event = "AA Data Available: From Sep 2008 Onward",
  date = as.Date("2008-09-01"),
  ypos = 0.7 
)

# Create the timeline plot
plot_timeline <-ggplot() +
  geom_segment(data = timeline_data, aes(x = start_date, xend = end_date, y = 0, yend = 0), size = 3, color = "blue") +
  geom_text(data = timeline_data, aes(x = start_date +  c(580, 650), y = 0.1, label = period), size = 5.5) +  # Adjusted x-coordinate
  geom_vline(data = rbind(event_data_1, event_data_2, event_data_3), aes(xintercept = date), linetype = "dashed", color = "red") +
  geom_text(data = event_data_1, aes(x = date+300, y = ypos, label = event), size = 4.5, color = "red", vjust = 1.5) +
  geom_text(data = event_data_2, aes(x = date, y = ypos, label = event), size = 4.5, color = "red", vjust = 1.5) +
  geom_text(data = event_data_3, aes(x = date+300, y = ypos, label = event), size = 4.5, color = "red", vjust = 1.5) +
  scale_x_date(date_breaks = "1 year", labels = function(x) ifelse(year(x) %in% 2011:2016, as.character(year(x) + 4), ifelse(year(x) %in% 2009:2010, "", as.character(year(x))))) +  # Adjusting x-axis labels for 2011-2016 and removing labels for 2009 and 2010
  scale_y_continuous(limits = c(-0.5, 0.7)) +  # Adjusting y-axis limits
  labs(x = "Date", y = "") +
  theme_minimal() +
  theme(
    axis.text.y = element_blank(),
    axis.ticks.y = element_blank(),
    plot.margin = margin(0.5, 0.5, 0.5, 0.5, "cm"),
    panel.grid.major.y = element_blank(),
    panel.grid.minor.y = element_blank(),
    plot.title = element_text(hjust = 0.5),
    axis.text.x = element_text(size = 14),  # Adjusted x-axis font size
    axis.title.x = element_text(size = 14)  # Adjusted x-axis title font size
  ) +
  geom_segment(aes(x = as.Date("2008-07-31"), xend = as.Date("2010-09-01"), y = -0.1, yend = -0.1), size = 2.5, linetype = "dotted", color = "black") +  # Adding thicker dotted line between two periods
  annotate(geom = "text", x = as.Date("2009-09-01"), y = -0.2, label = "Period of Non-Random Assignment", hjust = 0.5, size = 4.5)  # Adding text below "Amalgamation of Two Data Periods"

 
print(plot_timeline)
# Save the plot to a PDF file
ggsave(filename = "output/figures/sample_period.pdf", plot = plot_timeline, width = 12, height = 4)  # Adjusted width


